/**
 * Created by Administrator on 2016/5/21.
 */
$(document).ready(function(){
    showInfo();
    $('.main-menu ul .openable').click(function(){
        $('.main-menu ul .openable .submenu').css('display', 'block');
        $(this).find('ul').css('display', 'block');
    });
    $('.main-menu ul .showCurrent').click(function(){
        $('.main-menu ul .openable .submenu').css('display', 'none');
    });
    search();
    update();
});

function fillModifyUser(obj){
    if(!obj){
        return;
    }
    var $model = $("#modifyUser");
    $model.find("input[name=customerId]").val(obj.id);
    $model.find("input[name=name]").val(obj.name);
    $model.find("input[name=age]").val(obj.age);
    $model.find("select[name=gender]").val(obj.sex);
    $model.find("select[name=occupation]").val(obj.occupation);
}
function update(){
    $("#confirmModifyUser").click(function(){
        $.ajax({
            url: "/admin/user/modifyUser",
            type:'post',
            data:$('#modifyUserForm').serialize(),
            success:function(value){
                if(value == 1){
                    alert('修改成功');
                    $("#modifyUser").modal("hide");
                    //noinspection JSJQueryEfficiency
                    $("#userList tr").empty();
                    showInfo();
                }else{
                    alert('修改失败');
                }
            }
        });
    });
}
function showInfo(){
    $("#name").val("");
    $("#gender").val("");
    $("#age").val("");
    $("#occupation").val("");
    $.ajax({
        url: "/admin/user/getAllUser",
        type:'post',
        success:function(obj){
            var tbody = "";
            $.each(obj,function(n,value) {
                var trs = "";
                var gend = "男";
                if(value.gender == 2){
                    gend = "女";
                }
                trs += " <tr> " +
                " <td> " + value.name + "</td> " +
                " <td value="+ value.gender +"> " + gend + "</td> " +
                " <td> " + value.customerId + "</td> " +
                " <td> " + value.occupation + "</td> " +
                //" <td> <a data-toggle='modal' href='#modifyUser' class='modify' data-type='modify' data-id=' " + value.customerId + " '><u>修改</u>></a> " +
                " <td> <a href='javaScript:;' class='modify' data-type='modify' data-id=' " + value.customerId + " '><u>修改</u>></a> " +
                "</td>" +
                "</tr> ";
                tbody += trs;
            });
            $("#userList").append(tbody);
            //noinspection JSJQueryEfficiency
            $("#userList a.modify").on("click", function(){
                var $tr = $(this).closest("tr");
                var obj = {
                    id : $(this).data("id"),
                    name : $tr.find("td:eq(0)").text()||"",
                    sex :  $tr.find("td:eq(1)").attr("value")||1,
                    age : $.trim($tr.find("td:eq(2)").text())||"",
                    occupation : $.trim( $tr.find("td:eq(3)").text() )||""
                };
                $("#modifyUser").modal("show");
                fillModifyUser(obj);
            })
        }
    });
}
function search(){
    $("#searchBtn").click(function(){
        $.ajax({
            url: "/admin/user/searchUser",
            type:'post',
            data:$('#searchItem').serialize(),
            success:function(obj){
                var tbody = "";
                $.each(obj,function(n,value) {
                    var trs = "";
                    var gend = "男";
                    if(value.gender == 2){
                        gend = "女";
                    }
                    trs += " <tr> " +
                    " <td> " + value.name + "</td> " +
                    " <td> " + gend + "</td> " +
                    " <td> " + value.age + "</td> " +
                    " <td> " + value.occupation + "</td> " +
                    //" <td> <a data-toggle='modal' href='#modifyUser' class='modify' data-type='modify' data-id=' " + value.customerId + " '><u>修改</u>></a> " +
                    " <td> <a href='javaScript:;' class='modify' data-type='modify' data-id=' " + value.customerId + " '><u>修改</u>></a> " +
                    "</tr> ";
                    tbody += trs;
                });
                //noinspection JSJQueryEfficiency
                $("#userList tr").empty();
                //noinspection JSJQueryEfficiency
                $("#userList").append(tbody);
                //noinspection JSJQueryEfficiency
                $("#userList a.modify").on("click", function(){
                    var $tr = $(this).closest("tr");
                    var obj = {
                        id : $(this).data("id"),
                        name : $tr.find("td:eq(0)").text()||"",
                        sex :  $tr.find("td:eq(1)").attr("value")||1,
                        age : $.trim($tr.find("td:eq(2)").text())||"",
                        occupation : $.trim( $tr.find("td:eq(3)").text() )||""
                    };
                    $("#modifyUser").modal("show");
                    fillModifyUser(obj);
                })
            }
        });
    });
}
